Completed
Push — master ( eac4f3...3eb940 )
by Yannick
33:30
created

map.common.js ➔ map3dlimit   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/**
2
 * This javascript is part of FlightAirmap.
3
 *
4
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
5
 * Licensed under AGPL license.
6
 * For more information see: https://www.flightairmap.com/
7
*/
8
"use strict";
9
function getCookie(cname) {
10
    var name = cname + "=";
11
    var ca = document.cookie.split(';');
12
    for(var i=0; i<ca.length; i++) {
13
	var c = ca[i];
14
	while (c.charAt(0)==' ') c = c.substring(1);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
15
	if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
16
    }
17
    return "";
18
}
19
20
function delCookie(cname) {
21
    document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
22
}
23
24
function createCookie(name, value, days) {
25
    var date, expires;
26
    if (days) {
27
	date = new Date();
28
	date.setTime(date.getTime()+(days*24*60*60*1000));
29
	expires = "; expires="+date.toGMTString();
30
    } else {
31
	expires = "";
32
    }
33
    document.cookie = name+"="+value+expires+"; path=/";
34
}
35
36
function dynamicSort(property) {
37
    var sortOrder = 1;
38
    if(property[0] === "-") {
39
        sortOrder = -1;
40
        property = property.substr(1);
41
    }
42
    return function (a,b) {
43
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
44
        return result * sortOrder;
45
    }
46
}
47
48
function dynamicSortMultiple() {
49
    var props = arguments;
50
    return function (obj1, obj2) {
51
	var i = 0, result = 0, numberOfProperties = props.length;
52
	while(result === 0 && i < numberOfProperties) {
53
	    result = dynamicSort(props[i])(obj1, obj2);
54
	    i++;
55
	}
56
	return result;
57
    }
58
}
59
60
function map2dlimit(limit) {
61
    createCookie('map_2d_limit',limit,9999);
62
}
63
function map3dlimit(limit) {
64
    createCookie('map_3d_limit',limit,9999);
65
}
66
67
function mapType(selectObj) {
68
    var idx = selectObj.selectedIndex;
69
    var atype = selectObj.options[idx].value;
70
    var type = atype.split('-');
71
    if (type[0] == 'Mapbox') {
72
	createCookie('MapType',type[0],9999);
73
	createCookie('MapTypeId',type[1],9999);
74
	if (getCookie('Map2D3DSync')) {
75
	    createCookie('MapType3D',type[0],9999);
76
	    createCookie('MapType3DId',type[1],9999);
77
	}
78
    } else {
79
	createCookie('MapType',atype,9999);
80
	if (getCookie('Map2D3DSync')) {
81
	    createCookie('MapType3D',atype,9999);
82
	}
83
    }
84
    window.location.reload();
85
}
86
function mapType3D(selectObj) {
87
    var idx = selectObj.selectedIndex;
88
    var atype = selectObj.options[idx].value;
89
    var type = atype.split('-');
90
    if (type[0] == 'Mapbox') {
91
	createCookie('MapType3D',type[0],9999);
92
	createCookie('MapType3DId',type[1],9999);
93
	if (getCookie('Map2D3DSync')) {
94
	    createCookie('MapType',type[0],9999);
95
	    createCookie('MapTypeId',type[1],9999);
96
	}
97
    } else {
98
	createCookie('MapType3D',atype,9999);
99
	if (getCookie('Map2D3DSync')) {
100
	    createCookie('MapType',atype,9999);
101
	}
102
    }
103
    window.location.reload();
104
}
105
function clickSyncMap2D3D(cb) {
106
    createCookie('Map2D3DSync',cb.checked,9999);
107
    if (cb.checked) {
108
	createCookie('MapType3D',getCookie('MapType'),9999);
109
	createCookie('MapType3DId',getCookie('MapTypeId'),9999);
110
    }
111
}
112
113
function terrainType(selectObj) {
114
    var idx = selectObj.selectedIndex;
115
    var atype = selectObj.options[idx].value;
116
    var type = atype.split('-');
117
    document.cookie =  'MapTerrain='+type+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/'
118
    createCookie('MapTerrain',type,9999);
119
    if (type == 'stk') {
120
	stkterrain();
121
    } else if (type == 'articdem') {
122
	articterrain();
123
    } else if (type == 'ellipsoid') {
124
	ellipsoidterrain();
125
    } else if (type == 'vrterrain') {
126
	vrtheworldterrain();
127
    }
128
    //window.location.reload();
129
}
130
131
function sattypes(selectObj) {
132
    var sattypes = [], sattype;
133
    for (var i=0, len=selectObj.options.length; i< len;i++) {
134
	sattype = selectObj.options[i];
135
	if (sattype.selected) {
136
	    sattypes.push(sattype.value);
137
	}
138
    }
139
    createCookie('sattypes',sattypes.join(),2);
140
    updateSat();
141
}
142
function airlines(selectObj) {
143
    var airs = [], air;
144
    for (var i=0, len=selectObj.options.length; i< len;i++) {
145
	air = selectObj.options[i];
146
	if (air.selected) {
147
	    airs.push(air.value);
148
	}
149
    }
150
    createCookie('filter_Airlines',airs.join(),2);
151
}
152
function airlinestype(selectObj) {
153
    var idx = selectObj.selectedIndex;
154
    var airtype = selectObj.options[idx].value;
155
    createCookie('filter_airlinestype',airtype,2);
156
}
157
function racefilter(selectObj) {
158
    var idx = selectObj.selectedIndex;
159
    var race = selectObj.options[idx].value;
160
    if (race == 'all') {
161
	delCookie('filter_race');
162
    } else {
163
	createCookie('filter_race',race,2);
164
    }
165
    if (getCookie['MapFormat'] == '3d') {
166
	updateMarineData();
167
    } else {
168
	getLiveMarineData(0);
169
    }
170
}
171
function alliance(selectObj) {
172
    var idx = selectObj.selectedIndex;
173
    var alliance = selectObj.options[idx].value;
174
    createCookie('filter_alliance',alliance,2);
175
}
176
function identfilter() {
177
    var ident = $("#identfilter").value;
178
    createCookie('filter_ident',ident,2);
179
}
180
function mmsifilter() {
181
    var ident = $("#mmsifilter").value;
182
    createCookie('filter_mmsi',ident,2);
183
}
184
function removefilters() {
185
    // Get an array of all cookie names (the regex matches what we don't want)
186
    var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
187
    // Remove any that match the pattern
188
    for (var i = 0; i < cookieNames.length; i++) {
189
	if (/^filter_/.test(cookieNames[i])) {
190
	    delCookie(cookieNames[i]);
191
	}
192
    }
193
    window.location.reload();
194
}
195
function sources(selectObj) {
196
    var sources = [], source;
197
    for (var i=0, len=selectObj.options.length; i< len;i++) {
198
	source = selectObj.options[i];
199
	if (source.selected) {
200
	    sources.push(source.value);
201
	}
202
    }
203
    createCookie('filter_Sources',sources.join(),2);
204
}
205
206
207
function show2D() {
208
    createCookie('MapFormat','2d',10);
209
    if (document.getElementById("pointtype").className == 'tracker') {
210
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
211
    } else if (document.getElementById("pointtype").className == 'marine') {
212
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
213
    } else {
214
	createCookie('MapTrack',document.getElementById("pointident").className,1);
215
    }
216
    window.location.reload();
217
}
218
function show3D() {
219
    createCookie('MapFormat','3d',10);
220
    if (document.getElementById("pointtype").className == 'tracker') {
221
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
222
    } else if (document.getElementById("pointtype").className == 'marine') {
223
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
224
    } else {
225
	createCookie('MapTrack',document.getElementById("pointident").className,1);
226
    }
227
    window.location.reload();
228
}
229
function clickPolar(cb) {
230
    createCookie('polar',cb.checked,9999);
231
    window.location.reload();
232
}
233
function clickDisplayAirports(cb) {
234
    createCookie('displayairports',cb.checked,9999);
235
    window.location.reload();
236
}
237
function clickDisplayISS(cb) {
238
    createCookie('displayiss',cb.checked,9999);
239
    updateSat();
240
}
241
function clickDisplayMinimap(cb) {
242
    createCookie('displayminimap',cb.checked,9999);
243
    if (cb.checked == true) {
244
	displayMiniMap();
245
    } else {
246
	window.location.reload();
247
    }
248
}
249
function clickShadows(cb) {
250
    createCookie('map3dnoshadows',cb.checked,9999);
251
    viewer.shadows = cb.checked;
0 ignored issues
show
Bug introduced by
The variable viewer seems to be never declared. If this is a global, consider adding a /** global: viewer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
252
    //window.location.reload();
253
}
254
function clickSingleModel(cb) {
255
    createCookie('singlemodel',cb.checked,9999);
256
}
257
function clickUpdateRealtime(cb) {
258
    createCookie('updaterealtime',cb.checked,9999);
259
}
260
function clickVATSIM(cb) {
261
    createCookie('filter_ShowVATSIM',cb.checked,2);
262
}
263
function clickIVAO(cb) {
264
     createCookie('filter_ShowIVAO',cb.checked,2);
265
}
266
function clickphpVMS(cb) {
267
    createCookie('filter_ShowVMS',cb.checked,2);
268
}
269
function clickSBS1(cb) {
270
    createCookie('filter_ShowSBS1',cb.checked,2);
271
}
272
function clickBlocked(cb) {
273
    createCookie('filter_blocked',cb.checked,2);
274
}
275
function clickAPRS(cb) {
276
    createCookie('filter_ShowAPRS',cb.checked,2);
277
}
278
function clickDisplayGroundStation(cb) {
279
    createCookie('show_GroundStation',cb.checked,2);
280
    window.location.reload();
281
}
282
function clickDisplayWeatherStation(cb) {
283
    createCookie('show_WeatherStation',cb.checked,2);
284
    window.location.reload();
285
}
286
/*
287
function clickDisplayWeather(cb) {
288
    createCookie('show_Weather',cb.checked,2);
289
//    window.location.reload();
290
}
291
*/
292
function clickDisplayLightning(cb) {
293
    createCookie('show_Lightning',cb.checked,2);
294
    window.location.reload();
295
}
296
function clickDisplayFires(cb) {
297
    createCookie('show_Fires',cb.checked,2);
298
    window.location.reload();
299
}
300
function clickDisplay2DBuildings(cb) {
301
    createCookie('Map2DBuildings',cb.checked,2);
302
    window.location.reload();
303
}
304
305
function unitdistance(selectObj) {
306
    var idx = selectObj.selectedIndex;
307
    var unit = selectObj.options[idx].value;
308
    createCookie('unitdistance',unit,9999);
309
}
310
function unitspeed(selectObj) {
311
    var idx = selectObj.selectedIndex;
312
    var unit = selectObj.options[idx].value;
313
    createCookie('unitspeed',unit,9999);
314
    unitspeedvalue = unit;
0 ignored issues
show
Bug introduced by
The variable unitspeedvalue seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.unitspeedvalue.
Loading history...
315
}
316
function unitcoordinate(selectObj) {
317
    var idx = selectObj.selectedIndex;
318
    var unit = selectObj.options[idx].value;
319
    createCookie('unitcoordinate',unit,9999);
320
    unitcoordinate = unit;
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. unitcoordinate is already defined in line 316 as a function. While this will work, it can be very confusing.
Loading history...
321
}
322
function unitaltitude(selectObj) {
323
    var idx = selectObj.selectedIndex;
324
    var unit = selectObj.options[idx].value;
325
    createCookie('unitaltitude',unit,9999);
326
    unitaltitude = unit;
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. unitaltitude is already defined in line 322 as a function. While this will work, it can be very confusing.
Loading history...
327
}
328
329
function addarchive(begindate,enddate) {
330
    console.log('Add archive');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
331
    createCookie('archive',true,2);
332
    createCookie('archive_begin',begindate,2);
333
    createCookie('archive_end',enddate,2);
334
    createCookie('archive_speed',document.getElementById("archivespeed").value,2);
335
    window.location.reload();
336
}
337
function noarchive() {
338
    console.log('Exit archive!');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
339
    delCookie('archive');
340
    delCookie('archive_begin');
341
    delCookie('archive_end');
342
    delCookie('archive_speed');
343
    window.location.reload();
344
}
345
function msgbox(text,buttontext) {
346
	buttontext = buttontext || "OK";
347
	$("<div>" + text + "</div>").dialog({
348
	    dialogClass: "no-close",
349
	    buttons: [{
350
		text: buttontext,
351
		click: function() {
352
		    $( this ).dialog( "close" );
353
		    $(this).remove();
354
		}
355
	    }]
356
	});
357
}
358
function generateRandomPoint (latitude,longitude,height,diff,radius) {
359
360
	//console.log('height: '+height+' - diff: '+diff);
361
	radius = Math.random()*radius;
362
	latitude = latitude*(Math.PI/180.0);
363
	longitude = longitude*(Math.PI/180.0);
364
	
365
	const sinLat = 	Math.sin(latitude)
366
	const cosLat = 	Math.cos(latitude)
367
368
	/* go fixed distance in random direction*/
369
	const bearing = Math.random() * Math.PI*2
370
	const theta = radius/6371000
371
	const sinBearing = Math.sin(bearing)
372
	const cosBearing = Math.cos(bearing)
373
	const sinTheta = Math.sin(theta)
374
	const cosTheta = Math.cos(theta)
375
    
376
	latitude = Math.asin(sinLat*cosTheta+cosLat*sinTheta*cosBearing);
377
	longitude = longitude + Math.atan2( sinBearing*sinTheta*cosLat, cosTheta-sinLat*Math.sin(latitude ));
378
	/* normalize -PI -> +PI radians */
379
	longitude = ((longitude+(Math.PI*3))%(Math.PI*2))-Math.PI
380
	var h = height+(Math.random()*diff)
381
	//console.log('h: '+h);
382
	return {
383
	    latitude: latitude/(Math.PI/180.0),
384
	    longitude: longitude/(Math.PI/180.0),
385
	    height: h
386
	};
387
}
388
function getColor(colorStart,colorEnd,colorCount,step) {
389
	var alpha = (1.0/colorCount)*step;
390
	return {
391
	    r: colorStart[0]*alpha+(1-alpha)*colorEnd[0],
392
	    v: colorStart[1]*alpha+(1-alpha)*colorEnd[1],
393
	    b: colorStart[2]*alpha+(1-alpha)*colorEnd[2]
394
	};
395
}
396
function convertDMS(coord,latlong) {
397
	if (latlong == 'latitude') {
398
		var nsew = (coord >= 0) ? 'N' : 'S';
399
	} else if (latlong == 'longitude') {
400
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 398. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
401
	}
402
	var coord = Math.abs(coord);
403
	var deg = Math.floor(coord);
404
	var min = Math.floor((coord - deg) * 60);
405
	var sec = Math.round((coord - deg - min / 60) * 3600);
406
	var result = deg+"° "+min+"' "+sec+'" '+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 399 is false. Are you sure this can never be the case?
Loading history...
407
	return result;
408
}
409
function convertDM(coord,latlong) {
410
	if (latlong == 'latitude') {
411
		var nsew = (coord >= 0) ? 'N' : 'S';
412
	} else if (latlong == 'longitude') {
413
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 411. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
414
	}
415
	var coord = Math.abs(coord);
416
	var deg = Math.floor(coord);
417
	var min = Math.round((coord - deg) * 60 *1000)/1000;
418
	var result = deg+"° "+min+"' "+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 412 is false. Are you sure this can never be the case?
Loading history...
419
	return result;
420
}
421